package com.auemall.controller;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.auemall.constant.ApplicationConstant;
import com.auemall.po.BuyerGradesPO;
import com.auemall.po.TBuyerPO;
import com.auemall.service.BuyerService;
import com.auemall.utils.EUDataGridResult;
import com.auemall.utils.StringUtils;
import com.auemall.vo.BaseRespVO;
import com.auemall.vo.BuyerSearchVO;
import com.auemall.vo.BuyerVO;

/**
 * 用户管理Controller
 * <p>Title: UserController</p>
 * <p>Description: </p>
 *
 */

@Controller
@RequestMapping("/buyer/v1")
public class BuyerController {
    
    private Logger logger = Logger.getLogger(BuyerController.class);

	@Autowired
	private BuyerService buyerService;
	
	
	@RequestMapping(value="/search", method=RequestMethod.POST)
	@ResponseBody
	private EUDataGridResult searchUser(@RequestBody BuyerSearchVO req,int page, int rows) {
        EUDataGridResult result;
        try{
            result = buyerService.searchBuyer(req,page, rows);
        }catch(Exception e){
            result = new EUDataGridResult();
            result.setResult(ApplicationConstant.RESULT_FALTURE);
            result.setMsg("buyer/v1/search 查询会员列表失败："+e.getMessage());
            logger.error(result.getMsg());
        }
        return result;
	}
	
	/**
	 * 查看会员信息
	 * @param req
	 * @param flag
	 * @return
	 */
	@RequestMapping(value="/showBuyer", method=RequestMethod.POST)
    @ResponseBody
	public BuyerVO showBuyer(BuyerVO req){
		BuyerVO result = null;
		
		try {
			result = buyerService.selectByPrimaryKey(req.getId());	
		} catch (Exception e) {
			logger.error("查看会员信息失败");
		}	
		return result;
	}
	
	/**
	 * 
	 * @Title: addUser 
	 * @Description: TODO(添加用户)
	 * @param req
	 * @return BaseRespVO  
	 * @author wuyue
	 */
	@RequestMapping(value="/adduser", method=RequestMethod.POST)
    @ResponseBody
    public BaseRespVO addUser(@RequestBody BuyerVO req) {
		BaseRespVO respVO = new BaseRespVO();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        
        if(req.getBirthday()==null){
        	 respVO.setMsg("出生日期为空！");
             logger.error(respVO.getMsg());
             return respVO;
        }
        
        if(StringUtils.isBlank(req.getMobile())){
       	 respVO.setMsg("手机号为空！");
            logger.error(respVO.getMsg());
            return respVO;
       }
        
        TBuyerPO record = new TBuyerPO();
        BeanUtils.copyProperties(req, record);
        
        record.setRegtime(new Date());
        record.setPassword("123456");

        buyerService.insertSelective(record);
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("新增用户成功");
        return respVO;
    }
	
	
	@RequestMapping(value="/updateuser", method=RequestMethod.POST)
	@ResponseBody
	private BaseRespVO updateUser(@RequestBody BuyerVO req) {
	    BaseRespVO respVO;
	    try{
	        respVO = buyerService.updateByPrimaryKeySelective(req);
	    }catch(Exception e){
	        respVO = new BaseRespVO();
	        respVO.setResult(ApplicationConstant.RESULT_FALTURE);
	        respVO.setMsg("修改会员异常（/buyer/v1/updateuser）："+e.getMessage());
	        logger.error(respVO.getMsg());
	    }
	    return respVO;
	}
	
	/**
	 * 
	 * @Title: deleteItemBrand 
	 * @Description: TODO(删除会员)
	 * @param req
	 * @return BaseRespVO  
	 * @author WangChao
	 */
	@RequestMapping("/deleteuser/{id}")
	@ResponseBody
	private BaseRespVO deleteUser(@PathVariable String id) {
		
		
	    BaseRespVO respVO= new BaseRespVO();
	    try{
	         buyerService.deleteByPrimaryKey(id);
	         respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
	         respVO.setMsg("删除会员成功");
	    }catch(Exception e){
	        respVO = new BaseRespVO();
	        respVO.setResult(ApplicationConstant.RESULT_FALTURE);
	        respVO.setMsg("删除会员异常（/buyer/v1/deleteuser）："+e.getMessage());
	        logger.error(respVO.getMsg());
	    }
	    return respVO;
	}
	
	/**
	 * 
	 * @Title: selectBuyerGrade 
	 * @Description: TODO(查询会员级别列表)
	 * @param req
	 * @param page
	 * @param rows
	 * @return EUDataGridResult  
	 * @author WangChao
	 */
	@RequestMapping("/selectBuyerGrade")
    @ResponseBody
    public EUDataGridResult selectBuyerGrade(@RequestBody BuyerGradesPO req,int page, int rows) {
        EUDataGridResult result;
        try{
            result = buyerService.searchBuyerGrade(req, page, rows);
        }catch(Exception e){
            result = new EUDataGridResult();
            result.setResult(ApplicationConstant.RESULT_FALTURE);
            result.setMsg("item/v1/selectitembrand 查询会员级别列表失败："+e.getMessage());
            logger.error(result.getMsg());
        }
        return result;
	}
	
	/**
	 * 查看会员等级信息
	 * @param req
	 * @param flag
	 * @return
	 */
	@RequestMapping(value="/showBuyerGrade", method=RequestMethod.POST)
    @ResponseBody
	public BuyerGradesPO showBuyerGrade(BuyerGradesPO req){
		BuyerGradesPO result = null;
		try {
			result = buyerService.selectBuyerGradesByPrimaryKey(req.getId());
		} catch (Exception e) {
			logger.error("查看会员等级信息失败");
		}
		return result;
	}
	
	
	@RequestMapping(value="/addbuyerGrade", method=RequestMethod.POST)
    @ResponseBody
    public BaseRespVO addBuyerGrade(BuyerGradesPO req) {
		BaseRespVO result;
        try{
            result = buyerService.insertBuyerGradesSelective(req);
        }catch(Exception e){
            result = new EUDataGridResult();
            result.setResult(ApplicationConstant.RESULT_FALTURE);
            result.setMsg("buyer/v1/addbuyerGrade 新增会员等级失败："+e.getMessage());
            logger.error(result.getMsg());
        }
        return result;
    }
	
	@RequestMapping(value="/updatebuyerGrade", method=RequestMethod.POST)
    @ResponseBody
    public BaseRespVO updateBuyerGrade(BuyerGradesPO req) {
		BaseRespVO result;
        try{
            result = buyerService.updateBuyerGradesByPrimaryKeySelective(req);
        }catch(Exception e){
            result = new EUDataGridResult();
            result.setResult(ApplicationConstant.RESULT_FALTURE);
            result.setMsg("buyer/v1/addbuyerGrade 新增会员等级失败："+e.getMessage());
            logger.error(result.getMsg());
        }
        return result;
    }
	
	@RequestMapping("/deletebuyerGrade/{id}")
	@ResponseBody
	private BaseRespVO deleteBuyerGrade(@PathVariable String id) {
	    BaseRespVO respVO= new BaseRespVO();
	    try{
	         buyerService.deleteBuyerGradesByPrimaryKey(id);
	         respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
	         respVO.setMsg("删除会员成功");
	    }catch(Exception e){
	        respVO = new BaseRespVO();
	        respVO.setResult(ApplicationConstant.RESULT_FALTURE);
	        respVO.setMsg("删除会员异常（/buyer/v1/deleteuser）："+e.getMessage());
	        logger.error(respVO.getMsg());
	    }
	    return respVO;
	}
}
